<?php

require_once('include/header.php');
require_once('dao/ImagenesDAO.php');
require_once('logic/ImagenesLogic.php');
require_once('search/ImagenesSearchCriteria.php');

$logic = new ImagenesLogic($_POST);


// obtener action
$action = $_GET['action'];
if ($action == null){
	$action = $_POST['action'];
}

// obtengo ID del registro
$iditem = $_GET[iditem];
if ($iditem == "")
	$iditem = $_POST[iditem];

// obtengo table
$table = $_GET[t];
if ($table == "")
	$table = $_POST[t];	
	
switch ($action) {
	case "add":
		$result = $logic->save($_POST, $_FILES);
		if ($result == 'error') {
			$result = "error";
		} else {
			$result = "ok";
		}
		header( 'Location: imagenes.php?iditem=' . $iditem . '&t=' . $table );
		break;
		
	case "update":
		$result = $logic->update($_POST);
		if ($result == 'error') {
			$result = "error";
		} else {
			$result = "ok";
		}
		header( 'Location: imagenes.php?iditem=' . $iditem . '&t=' . $table );
		break;
		
	case "delete":
		$result = $logic->delete($_POST);
		if ($result == 'error'){$smarty->assign('error', "Error al borrar la imagen");}
		header( 'Location: imagenes.php?iditem=' . $iditem . '&t=' . $table );
		break;	
}


// obtengo todas las imagenes
$imageSearchCriteria = new ImagenesSearchCriteria();
$imageSearchCriteria->setIditem($iditem);
$imageSearchCriteria->setTable($table);
$images = $logic->find($imageSearchCriteria);

$entidad = $logic->getEntidad($table, $iditem); 

$smarty->assign('iditem',$iditem);
$smarty->assign('t',$table);
$smarty->assign('images',$images);
$smarty->assign('entidad',$entidad);

$smarty->assign("detailPage", "imagenes.tpl");
$smarty->display("admin_generic.tpl");


